import React, { useReducer } from 'react';

const myContext = React.createContext();

const stateAll = {
    myClueType: 1, //我的线索类型
    itemStatus: 0, //项目状态
    itemType: 1, //项目类型
    cluesType: 1, //线索池类型0:已分配,1:未分配,2:已搁置
    todoListType: 0, //待办事项类型：0:全部,1:待我审批,2:已审批,3:挂起
    myApproval: 1, //我的审批类型：1：全部
    contractType: 0, //合同列表类型： 0:签约,1:执行中,2:历史合同, 4: 全部
    paymentType: 0, //财务管理支付列表类型 0: 等待支付， 1: 已经支付
    productionPlanType: 0, //生产计划 0: 执行中 1: 挂起 2: 待下发列表
    awaitPlanType: 0, //待办计划 0: 我的任务 1: 部门任务
    tokenType: 1, //token 状态 1： oa 2： wx
}

//更改我的线索类型
function reducer(state = stateAll, action) {
    let newState = JSON.parse(JSON.stringify(state));
    switch(action.type) {
        case 'myClueType':
            newState.myClueType = action.myClueType;
            return newState;
        case 'itemMonagement':
            newState.itemStatus = action.itemStatus;
            newState.itemType = action.itemType;
            return newState;
        case 'cluesAll': //线索池
            newState.cluesType = action.cluesType;
            return newState;
        case 'todoList': //代办事项
            newState.todoListType = action.todoListType;
            return newState;
        case 'myApproval': //我的审批
            newState.myApprovalType = action.myApprovalType;
            return newState;
        case 'contract': //合同type
            newState.contractType = action.contractType;
            return newState;
        case 'payment': //财务管理 -> 支付列表type
            newState.paymentType = action.paymentType;
            return newState;
        case 'productionPlan': //生产计划 -> 生产计划
            newState.productionPlanType = action.productionPlanType;
            return newState;
        case 'awaitPlan': //生产计划 -> 待办任务
            newState.awaitPlanType = action.awaitPlanType;
            return newState;
        case 'tokenType': //生产计划 -> 待办任务
            newState.tokenType = action.value;
            return newState;
        default:
            return state;
    }
}

const ContextProvider = props => {
    const [state, dispatch] = useReducer(reducer, stateAll);
    return <myContext.Provider value = {{state, dispatch}}>
        {props.children}
    </myContext.Provider>
}

export { reducer, myContext, ContextProvider}